﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Net;
namespace Engine.NetClasses
{
    public enum TypeLog
    {
         SendLog,
        ReciveLog,
         StateLog,
        GlobalLog,
    }
    public class ServerLogs:IDisposable
    {
        private ServerOptions m_serverOptions;
        private Stream SendLog;
        private Stream ReciveLog;
        private Stream StateLog;
        private Stream GlobalLog;
        public ServerLogs(ServerOptions m_serverOptions)
        {
            // TODO: Complete member initialization
            this.m_serverOptions = m_serverOptions;
            DateTime dt = DateTime.Now;
            SendLog = File.Open(m_serverOptions.BaseLogDir + m_serverOptions.SendLogDir + "Sendlog" + dt.ToString("yyyyMMddHHmmss") + ".log", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            ReciveLog = File.Open(m_serverOptions.BaseLogDir + m_serverOptions.ReceiveLogDir + "ReciveLog" + dt.ToString("yyyyMMddHHmmss") + ".log", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StateLog = File.Open(m_serverOptions.BaseLogDir + m_serverOptions.StateLogDir + "StateLog" + dt.ToString("yyyyMMddHHmmss") + ".log", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            GlobalLog = File.Open(m_serverOptions.BaseLogDir + "ServerLog" + dt.ToString("yyyyMMddHHmmss") + ".log", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            WriteLineLog(GlobalLog, @"/----------------------------------------------------------------------------------------------------/");
            WriteLineLog(GlobalLog, "Server log file: '" + m_serverOptions.BaseLogDir + "ServerLog" + dt.ToString("yyyyMMddHHmmss") + ".log'");
            WriteLineLog(GlobalLog, "State log file: '" + m_serverOptions.BaseLogDir + m_serverOptions.StateLogDir + "StateLog" + dt.ToString("yyyyMMddHHmmss") + ".log'");
            WriteLineLog(GlobalLog, "Recive log file: '" + m_serverOptions.BaseLogDir + m_serverOptions.ReceiveLogDir + "ReciveLog" + dt.ToString("yyyyMMddHHmmss") + ".log'");
            WriteLineLog(GlobalLog, "Send log file: '" + m_serverOptions.BaseLogDir + m_serverOptions.SendLogDir + "Sendlog" + dt.ToString("yyyyMMddHHmmss") + ".log'");
            WriteLineLog(GlobalLog, @"/----------------------------------------------------------------------------------------------------/");
        }
        private void WriteLineLog(Stream stream, string log)
        {
            StreamWriter sw = new StreamWriter(stream);
            sw.WriteLine(GetDateTime() + log);
            sw.Flush();
            stream.Flush();
            
        }
        public void WriteLineLog(TypeLog typeLog, string log)
        {
            switch (typeLog)
            {
                case TypeLog.SendLog:
                    WriteLineLog(SendLog, log);
                    break;
                case TypeLog.ReciveLog:
                    WriteLineLog(ReciveLog, log);
                    break;
                case TypeLog.StateLog:
                    WriteLineLog(StateLog, log);
                    break;
                case TypeLog.GlobalLog:
                    WriteLineLog(GlobalLog, log);
                    break;
                default:
                    break;
            }
        }
        public string GetDateTime()
        {
            return DateTime.Now.ToString() + "::";
        }
        private string ReadLine(Stream stream)
        {
            stream.Seek(0, SeekOrigin.Begin);
            StreamReader sr = new StreamReader(stream);
            
            

           return sr.ReadToEnd();
        }
        public string ReadLogs(int ID)
        {
            StringBuilder sb = new StringBuilder();
            List<string> tmp = new List<string>();
            string log = ReadLine(GlobalLog);
            string[] tsplit = log.Split(Environment.NewLine.ToCharArray());
            for (int i = 0; i < tsplit.Length; i++)
            {
                if (tsplit[i].IndexOf("ID='"+ID+"'")>0)
                {
                    tmp.Add(tsplit[i]);
                }
            }
             log = ReadLine(StateLog);
            tsplit = log.Split(Environment.NewLine.ToCharArray());
            for (int i = 0; i < tsplit.Length; i++)
            {
                if (tsplit[i].IndexOf("ID='" + ID + "'") > 0)
                {
                    tmp.Add(tsplit[i]);
                }
            }
            log = ReadLine(SendLog);
            tsplit = log.Split(Environment.NewLine.ToCharArray());
            for (int i = 0; i < tsplit.Length; i++)
            {
                if (tsplit[i].IndexOf("ID='" + ID + "'") > 0)
                {
                    tmp.Add(tsplit[i]);
                }
            }
            log = ReadLine(ReciveLog);
            tsplit = log.Split(Environment.NewLine.ToCharArray());
            for (int i = 0; i < tsplit.Length; i++)
            {
                if (tsplit[i].IndexOf("ID='" + ID + "'") > 0)
                {
                    tmp.Add(tsplit[i]);
                }
            }
            tmp.Sort();
            for (int i = 0; i < tmp.Count; i++)
			{
                sb.AppendLine(tmp[i]);
			}
            return sb.ToString();
        }
        public void Close()
        {
            Dispose();
        }
        public void Dispose()
        {
            SendLog.Close();
            ReciveLog.Close();
            StateLog.Close();
            GlobalLog.Close();
           
        }
    }
}
